home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH01 / HEXCONV / HEXCONVU.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1996-04-06  |  7.9 KB  |  257 lines

  1. (*****************************************************************************)
  2. (*                                                                           *)
  3. (* HexConv                                                                   *)
  4. (*                                                                           *)
  5. (* 10/25/95                                                                  *)
  6. (* Randall L. Hyde                                                           *)
  7. (* Copyright 1995, All Rights Reserved Unless Otherwise Noted                *)
  8. (*                                                                           *)
  9. (* This program allows the user to enter data in signed decimal, unsigned    *)
  10. (* decimal, unsigned hexadecimal, and unsigned binary formats (all 16 bits). *)
  11. (* It automatically converts an input in one radix to its corresponding      *)
  12. (* representation in the other bases.                                        *)
  13. (*                                                                           *)
  14. (* Runs under Windows 3.1, Windows 95, and Windows NT.                       *)
  15. (* Source Code: Borland Delphi (object Pascal).                              *)
  16. (*                                                                           *)
  17. (*****************************************************************************)
  18.  
  19.  
  20. unit Hexconvu;
  21.  
  22. interface
  23.  
  24. uses
  25.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  26.   Forms, Dialogs, StdCtrls, ExtCtrls, Converts;
  27.  
  28. type
  29.  
  30.   { Class definition for the conversion form }
  31.  
  32.   THexConv = class(TForm)
  33.     BoundingBox: TGroupBox;
  34.  
  35.     BinLabel: TLabel;
  36.     HexLabel: TLabel;
  37.     DecLabel: TLabel;
  38.     UnsignedDecLbl: TLabel;
  39.  
  40.     BinEntry: TEdit;     { Box into which the user enters binary data         }
  41.     HexEntry: TEdit;     { Box into which the user enters hexadecimal data    }
  42.     DecEntry: TEdit;     { Box into which the user enters signed decimal data }
  43.     UnsignedEntry: TEdit;{ Box into which the user enters unsigned data       }
  44.  
  45.     ExitBtn: TButton;
  46.     ClearBtn: TButton;
  47.     AboutBtn: TButton;
  48.  
  49.     procedure ExitBtnClick(Sender: TObject);
  50.     procedure ClearBtnClick(Sender: TObject);
  51.     procedure AboutBtnClick(Sender: TObject);
  52.     procedure DecEntryKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  53.     procedure HexEntryKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  54.     procedure BinEntryKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  55.     procedure UnsignedEntryKeyUp(   Sender: TObject;
  56.                                     var Key: Word;
  57.                                     Shift: TShiftState
  58.                                  );
  59.     procedure BoundingBoxClick(Sender: TObject);
  60.  
  61.   end;
  62.  
  63.  
  64.  
  65.  
  66. {Instance variable for this form }
  67.  
  68. var
  69.   HexConv: THexConv;
  70.  
  71.  
  72.  
  73.  
  74. implementation
  75.  
  76. {$R *.DFM}
  77.  
  78.  
  79.  
  80. { When the user presses the Quit button, the following method terminates }
  81. { the program.                                                           }
  82.  
  83. procedure THexConv.ExitBtnClick(Sender: TObject);
  84. begin
  85.      Halt;
  86. end;
  87.  
  88. { If the user presses the clear button, the following method sets all the }
  89. { fields to zeros.                                                        }
  90.  
  91. procedure THexConv.ClearBtnClick(Sender: TObject);
  92. begin
  93.  
  94.      DecEntry.Text := '0';
  95.      UnsignedEntry.Text := '0';
  96.      HexEntry.Text := '0000';
  97.      BinEntry.Text := '0000 0000 0000 0000';
  98.  
  99. end;
  100.  
  101.  
  102.  
  103. { When the user presses (and releases) a key inside the decimal entry     }
  104. { text box, the following method checks the resulting string to see if it }
  105. { is a valid decimal integer value.  If so, this method updates the       }
  106. { strings in all the edit boxes to reflect the new value.  If the result- }
  107. { ing value is invalid, the following method turns the decimal data entry }
  108. { box red.                                                                }
  109.  
  110. procedure THexConv.DecEntryKeyUp(Sender: TObject; var Key: Word;
  111.   Shift: TShiftState);
  112. var  Value:integer;
  113. begin
  114.  
  115.      if (CheckDec(DecEntry.Text)) then begin  { Legal decimal value       }
  116.  
  117.         Value := StrToInt(DecEntry.Text);     {Convert string to integer  }
  118.  
  119.         { Update the strings in the other three data entry boxes.         }
  120.  
  121.         HexEntry.Text := IntToHex(Value,4);
  122.         BinEntry.Text := IntToBin(Value,16);
  123.         UnsignedEntry.Text := IntToStr(word(Value));
  124.  
  125.         { Change the color back to normal in case it was red before.      }
  126.         { Red denotes an error, a condition that no longer exists.        }
  127.  
  128.         DecEntry.Color := clWindow;
  129.  
  130.      end
  131.      else begin        { Illegal decimal value }
  132.  
  133.           { If we have an illegal decimal value, beep the speaker and turn }
  134.           { the background color in the text entry box to red.             }
  135.  
  136.           MessageBeep($ffff);
  137.           DecEntry.Color := clRed;
  138.  
  139.      end;
  140.  
  141. end;
  142.  
  143.  
  144. { The following method processes keystrokes in the unsigned decimal entry  }
  145. { box.  This routine is nearly identical to the DecEntryKeyUp event method.}
  146. { Please see the comments  in that routine for more details.               }
  147.  
  148. procedure THexConv.UnsignedEntryKeyUp( Sender: TObject;
  149.                                        var Key: Word;
  150.                                        Shift: TShiftState);
  151. var  Value:word;
  152. begin
  153.  
  154.      if (CheckUnsigned(UnsignedEntry.Text)) then begin
  155.  
  156.         Value := StrToInt(UnsignedEntry.Text);
  157.         HexEntry.Text := IntToHex(Value,4);
  158.         DecEntry.Text := IntToStr(integer(Value));
  159.         BinEntry.Text := IntToBin(Value,16);
  160.         UnsignedEntry.Color := clWindow;
  161.  
  162.      end
  163.      else begin
  164.  
  165.           MessageBeep($ffff);
  166.           UnsignedEntry.Color := clRed;
  167.  
  168.      end;
  169.  
  170. end;
  171.  
  172.  
  173. { The following method processes keystrokes in the hexadecimal entry       }
  174. { box.  This routine is nearly identical to the DecEntryKeyUp event method.}
  175. { Please see the comments  in that routine for more details.               }
  176.  
  177. procedure THexConv.HexEntryKeyUp(Sender: TObject;
  178.                                  var Key: Word;
  179.                                  Shift: TShiftState);
  180. var value:integer;
  181. begin
  182.  
  183.      if (CheckHex(HexEntry.Text)) then begin
  184.  
  185.         Value := HexToInt(HexEntry.Text);
  186.         DecEntry.Text := IntToStr(Value);
  187.         BinEntry.Text := IntToBin(Value,16);
  188.         UnsignedEntry.Text := IntToStr(word(Value));
  189.         HexEntry.Color := clWindow;
  190.  
  191.      end
  192.      else begin
  193.  
  194.           MessageBeep($ffff);
  195.           HexEntry.Color := clRed;
  196.  
  197.      end;
  198.  
  199. end;
  200.  
  201.  
  202.  
  203. { The following method processes keystrokes in the binary data entry       }
  204. { box.  This routine is nearly identical to the DecEntryKeyUp event method.}
  205. { Please see the comments  in that routine for more details.               }
  206.  
  207. procedure THexConv.BinEntryKeyUp(Sender: TObject; var Key: Word;
  208.   Shift: TShiftState);
  209. var value:integer;
  210. begin
  211.  
  212.      if (CheckBin(BinEntry.Text)) then begin
  213.  
  214.         Value := BinToInt(BinEntry.Text);
  215.         DecEntry.Text := IntToStr(Value);
  216.         HexEntry.Text := IntToHex(Value,4);
  217.         UnsignedEntry.Text := IntToStr(word(Value));
  218.         BinEntry.Color := clWindow;
  219.  
  220.      end
  221.      else begin
  222.  
  223.           MessageBeep($ffff);
  224.           BinEntry.Color := clRed;
  225.  
  226.      end;
  227.  
  228. end;
  229.  
  230.  
  231.  
  232.  
  233. { Pressing the 'about' button brings up a dialog box with the copyright    }
  234. { information.                                                             }
  235.  
  236. procedure THexConv.AboutBtnClick(Sender: TObject);
  237. begin
  238.  
  239.     MessageDlg(
  240.        'Hex/Decimal/Binary Converter, Copyright 1995 by Randall Hyde',
  241.        mtInformation, [mbOk], 0);
  242.  
  243. end;
  244.  
  245. procedure THexConv.BoundingBoxClick(Sender: TObject);
  246. var value:word;
  247. begin
  248.         Value := word(BinToInt(BinEntry.Text));
  249.         BinEntry.Text := IntToBin(Value,16);
  250.         DecEntry.Text := IntToStr(integer(Value));
  251.         HexEntry.Text := IntToHex(Value,4);
  252.         UnsignedEntry.Text := IntToStr(word(Value));
  253.  
  254. end;
  255.  
  256. end.
  257.